﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace FlexCapture
{
    public class FlexEncoder
    {
        private unsafe void* XEncoder = null;
        private static FlexEncoder flexEncoder = null;


        [DllImport("DFlex.dll")]
        private static unsafe extern void* Flex_New();

        public unsafe FlexEncoder()
        {
            XEncoder = Flex_New();
        }

        [DllImport("DFlex.dll")]
        private static unsafe extern int Flex_SetFlexInfo(void* context, System.IntPtr hWnd);

        public unsafe int SetFlexInfo(System.IntPtr hWnd)
        {
            int returnValue = 0;
            try
            {
                returnValue = Flex_SetFlexInfo(XEncoder, hWnd);
            }
            catch{
                returnValue = 0;
            }
            return returnValue;
        }

        [DllImport("DFlex.dll")]
        private static unsafe extern int Flex_StartCapture(void* context, string highMediaFullPath, string audioFullPath, string lowMediaFullPath);

        public unsafe int StartCapture(string highMediaFullPath, string audioFullPath, string lowMediaFullPath)
        {
            Flex_StartCapture(XEncoder, highMediaFullPath, audioFullPath, lowMediaFullPath);
            return 1;
        }

        [DllImport("DFlex.dll")]
        private static unsafe extern int Flex_StopCapture(void* context);

        public unsafe int StopCapture()
        {
            Flex_StopCapture(XEncoder);
            return 1;
        }

        [DllImport("DFlex.dll")]
        private static unsafe extern int Flex_GetFrameCount(void* context, ref int CurrentFrames);

        public unsafe int GetFrameCount(ref int CurrentFrames)
        {
            Flex_GetFrameCount(XEncoder, ref CurrentFrames);
            return 1;
        }

        [DllImport("DFlex.dll")]
        private static unsafe extern int Flex_GetTotalFrameCount(void* context, ref int TotalFrame);

        public unsafe int GetTotalFrameCount(ref int TotalFrame)
        {
            Flex_GetTotalFrameCount(XEncoder, ref TotalFrame);
            return 1;
        }

        [DllImport("DFlex.dll")]
        private static unsafe extern int Flex_GetVol(void* context, ref int leftVol, ref int rightVol);

        public unsafe int GetTotalFrameCount(ref int leftVol, ref int rightVol)
        {
            Flex_GetVol(XEncoder, ref leftVol, ref rightVol);
            return 1;
        }

        public static FlexEncoder CreateInstance()
        {
            if (flexEncoder == null)
            {
                flexEncoder = new FlexEncoder();
            }
            return flexEncoder;
        }
    }
}
